package com.aptemo.webetek.client.widgets.payments;

import java.util.List;
import java.util.MissingResourceException;

import com.aptemo.webetek.client.Webetek;
import com.aptemo.webetek.client.enums.EnumPaymentType;
import com.aptemo.webetek.client.utils.TokenUtils;
import com.aptemo.webetek.client.widgets.paging.PagingDisplay;
import com.aptemo.webetek.client.widgets.paging.PagingView;
import com.aptemo.webetek.shared.PaymentBean;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.HasChangeHandlers;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTMLTable;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;

public class Payments
		extends Composite implements PaymentsDisplay
{

	private static PaymentsUiBinder	uiBinder	= GWT.create(PaymentsUiBinder.class);

	interface PaymentsUiBinder extends UiBinder<Widget, Payments>
	{
	}

	@UiField
	FlexTable	contentPanel;

	@UiField
	Label		infoField;

	@UiField
	PagingView	pagingPanel;

	@UiField
	ListBox		statusFilter;

	public Payments()
	{
		initWidget(uiBinder.createAndBindUi(this));

		// init enum
		for (EnumPaymentType item : EnumPaymentType.values())
		{
			statusFilter.addItem(translate(item.toString()), item.toString());
		}

	}

	@Override
	public void updatePaymentList(List<PaymentBean> list)
	{
		contentPanel.removeAllRows();
		if (list.size() == 0)
		{
			pagingPanel.setVisible(false);
			contentPanel.setText(0, 0, Webetek.getConstants().noEntries());
			return;
		}
		pagingPanel.setVisible(true);

		int index = 0;
		//contentPanel.setWidget(0, 0, widget);
		for (PaymentBean item : list)
		{
			// contentPanel.add(item);
			// StakeRow row = new StakeRow(item);
			// StakeRowPresenter pr = new StakeRowPresenter(item, null, row,
			// null);
			// pr.go(null);
			PaymentRow row = new PaymentRow(item);
			contentPanel.setWidget(index++, 0, row);
		}
	}

	@Override
	public Widget getDisplay()
	{
		return this;
	}

	@Override
	public HasText getInfoField()
	{
		return infoField;
	}

	@Override
	public HasClickHandlers getList()
	{
		return contentPanel;
	}

	@Override
	public int getClickedRow(ClickEvent event)
	{
		int selectedRow = -1;
		HTMLTable.Cell cell = contentPanel.getCellForEvent(event);

		if (cell != null)
		{
			// Suppress clicks if the user is actually selecting the
			// check box
			//
			if (cell.getCellIndex() >= 0)
			{
				selectedRow = cell.getRowIndex();
			}
		}

		return selectedRow;
	}

	@Override
	public PagingDisplay getPagingView()
	{
		return pagingPanel;
	}

	@Override
	public HasChangeHandlers getStatusFilter()
	{
		return statusFilter;
	}

	@Override
	public void setStatusFilterSelected(EnumPaymentType type)
	{
		for (int i = 0; i < statusFilter.getItemCount(); i++)
		{
			String val = statusFilter.getValue(i);

			if (!val.isEmpty() && EnumPaymentType.valueOf(val) != null && EnumPaymentType.valueOf(val).equals(type))
			{
				statusFilter.setSelectedIndex(i);
				return;
			}
		}

	}

	@Override
	public EnumPaymentType getStatusFilterValue()
	{
		int i = statusFilter.getSelectedIndex();

		String s = statusFilter.getValue(i);
		if (s.isEmpty())
		{
			return null;
		}
		return EnumPaymentType.valueOf(s);
	}

	@Override
	public String translate(String key)
	{
		try
		{
			return Webetek.getConstants().getString(key);
		}
		catch (MissingResourceException e)
		{
			return key;
		}
	}

	@UiHandler("statusFilter")
	void onStatusChanged(ChangeEvent event)
	{
		String token = History.getToken();
		if (TokenUtils.getTokenPart(token, "payments").isEmpty())
		{
			// do nothing...
			return;
		}

		if (getStatusFilterValue() != null)
		{
			token = TokenUtils.setToken(token, "filter", getStatusFilterValue().toString());
		}
		else
		{
			token = TokenUtils.setToken(token, "filter", "");
			// token = TokenUtils.removeToken(token, "filter");
		}
		// reset paging...
		token = TokenUtils.removeToken(token, "page");
		History.newItem(token, false);
	}

}
